package com.edgar.common.lib.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by edgar on 2015/11/14.
 */
public final class ViewHolder {
    private Context mContext;
    private int mPosition;
    private View mItemView;

    private ViewHolder(Context context, int position, View itemView){
        this.mContext = context;
        this.mPosition = position;
        this.mItemView = itemView;
    }

    public static ViewHolder bindViewHolder(Context context,View connerView,int position,int resId){
        ViewHolder viewHolder = null;
        if(connerView == null){
            connerView = LayoutInflater.from(context).inflate(resId,null);
            viewHolder = new ViewHolder(context,position,connerView);
        } else {
            viewHolder = (ViewHolder) connerView.getTag();
            viewHolder.mPosition = position;
        }
        return viewHolder;
    }

    public <T extends View> T findViewById(int resId){
        return (T) mItemView.findViewById(resId);
    }

    public <T extends View> T findViewWithTag(Object tag){
        return (T) mItemView.findViewWithTag(tag);
    }

    public <T extends TextView> T findTextViewById(int resId){
        return (T) mItemView.findViewById(resId);
    }

    public ViewHolder setText(int resId,Spanned spanned){
        TextView textView = findTextViewById(resId);
        if(textView != null) textView.setText(spanned);
        return this;
    }

    public ViewHolder setText(int resId,CharSequence charSequence){
        TextView textView = findTextViewById(resId);
        if(textView != null) textView.setText(charSequence);
        return this;
    }

    public ViewHolder setTextColor(int resId,int color){
        TextView textView = findViewById(resId);
        if(textView != null) textView.setTextColor(color);
        return this;
    }

    public ViewHolder setImageResource(int resId,int drawableId){
        ImageView imageView = findViewById(resId);
        if(imageView != null) imageView.setImageResource(drawableId);
        return this;
    }

    public ViewHolder setImageDrawable(int resId,Drawable drawable){
        ImageView imageView = findViewById(resId);
        if(imageView != null) imageView.setImageDrawable(drawable);
        return this;
    }

    public ViewHolder setOnClickListener(int resId,View.OnClickListener listener){
        View view = findViewById(resId);
        view.setOnClickListener(listener);
        return this;
    }

    public ViewHolder setOnLongListener(int resId,View.OnLongClickListener listener){
        View view = findViewById(resId);
        if(view == null) view.setOnLongClickListener(listener);
        return this;
    }

    public View getConnerView(){
        return mItemView;
    }

    public int getPosition(){
        return mPosition;
    }
}